package cn.lutu.domain.service.impl;

import cn.lutu.cache.OrderCacheRepository;
import cn.lutu.domain.repository.InvoiceRepository;
import cn.lutu.domain.repository.OrderRepository;
import cn.lutu.domain.service.OrderService;
import cn.lutu.protocol.entity.Order;
import cn.lutu.protocol.vo.OrderListVO;
import cn.lutu.protocol.vo.OrderVO;
import cn.lutu.protocol.vo.adminVO.AdminOrderVO;
import cn.lutu.util.SysConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @date 2023-06-03
 * @author luoyinhan
 * */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderRepository orderRepository;

    @Autowired
    InvoiceRepository invoiceRepository;

    @Autowired
    OrderCacheRepository orderCacheRepository;

    @Override
    @Transactional
    public void deleteOrder(Long id) {
        log.debug("删除订单业务,参数为{}",id);
        orderRepository.deleteById(id);

        invoiceRepository.deleteByOrderId(id);
    }

    @Override
    public void cancelOrder(Long id) {
        log.debug("取消订单业务,参数为{}",id);
        Order order=new Order();
        order.setId(id);
        order.setStatus(SysConstant.CANCEL_SUCCESS);
        orderRepository.updateById(order);
    }

    @Override
    public void refundOrder(Long id) {
        log.debug("订单退款业务,参数为{}",id);
        Order order=new Order();
        order.setId(id);
        order.setStatus(SysConstant.REFUD_SUCCESS);
        orderRepository.updateById(order);
    }

    @Override
    public List<OrderVO> getListOrderByStatus(String status,Long id) {
        log.debug("查看用户订单业务,参数为{},{}",status,id);
        return orderRepository.selectListOrderByStatus(status,id);
    }

    @Override
    public List<OrderVO> getListByCategoryAndKeyword(Long categoryId, String keyword, Long id) {
        log.debug("搜索用户订单业务,参数为{},{},{}",categoryId,keyword,id);
        return orderRepository.selectListOrderByCategoryAndKeyword(categoryId,keyword,id);
    }

    @Override
    public List<OrderListVO> getAllOrderList() {
        log.debug("查看所有用户订单");
        return orderCacheRepository.readOrder();
    }

    @Override
    public void rebuildListCache() {
        log.debug("开始处理重建缓存中的订单列表的业务");
        orderCacheRepository.delete();
        List<OrderListVO> list = orderRepository.list();
        orderCacheRepository.insertRedis(list);
    }
}
